<html>
<head>
<title>firstworks   Programming with SQL Relay using the PHP Pear DB API</title>
<link href="../css/styles.css" rel="stylesheet">
</head>
<body>

<span class="heading1">Programming with SQL Relay using the PHP Pear DB API</span><br><br>

<ul>
<li><a href="#session">Establishing a Sessions</a></li>
<li><a href="#query">Executing Queries</a></li>
<li><a href="#commit">Commits and Rollbacks</a></li>
<li><a href="#temptables">Temporary Tables</a></li>
<li><a href="#errors">Catching Errors</a></li>
<li><a href="#bindvars">Bind Variables</a></li>
<li><a href="#rebinding">Re-Binding and Re-Executing</a></li>
<li><a href="#fields">Accessing Fields in the Result Set</a></li>
<li><a href="#cursors">Cursors</a></li>
<li><a href="#columns">Getting Column Information</a></li>
<li><a href="#storedprocedures">Stored Procedures</a></li>
<li><a href="#lastinsertid">Getting the Last Insert ID in SQLite</a></li>
</ul>

<a name="session"></a>
<span class="heading2">Establishing a Session</span><br><br>

<p>To use SQL Relay, you have to identify the connection that you intend to 
use.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>...</b></font> execute some queries <font color="#a52829"><b>...</b></font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>In this connect string, the following components have the following
meanings:</p>

<blockquote>
<table>
<tr><td align="right" valign="top"><b>testuser</b></td>
<td>- the username to use to connect to the
SQL Relay server, corresponds to the username in an entry in the "users"
tag of the sqlrelay.conf file</td></tr>
<tr><td align="right" valign="top"><b>testpassword</b></td>
<td>- the password to use to connect to
the SQL Relay server, corresponds to the password in an entry in the "users"
tag of the sqlrelay.conf file</td></tr>
<tr><td align="right" valign="top"><b>testhost</b></td>
<td>- the hostname that the SQL Relay
server is running on</td></tr>
<tr><td align="right" valign="top"><b>9000</b></td>
<td>- the port that the SQL Relay server is
listening on</td></tr>
<tr><td align="right" valign="top"><b>testdb</b></td>
<td>- this parameter is ignored; the pear DB
spec requires that a database name be included in the connect string, but
the database that SQL Relay is connected to is defined in the sqlrelay.conf
file</td></tr>
</table>
</blockquote>

<p>After calling the constructor, a session is established when the first 
execute() is run.</p>

<p>For the duration of the session, the client stays connected to a database
connection daemon.  While one client is connected, no other client can connect.
Care should be taken to minimize the length of a session.</p>

<p>If you're using a transactional database, ending a session has a catch.
Database connection daemons can be configured to send either a commit or
rollback at the end of a session if DML queries were executed during the
session with no commit or rollback.  Program accordingly.</p>

<a name="query"></a>
<span class="heading2">Executing Queries</span><br><br>

<p>Call query() to run a query.  query() will return DB_OK for successful
DML or DDL queries or a result set for select queries.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">create table testtable (testnumber number, testchar char(40), testvarchar varchar2(40), testdate date, testlong long)</font>&quot;<font color="#6b59ce">)</font><font color="#a52829"><b>==</b></font>DB_OK<font color="#6b59ce">)</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">insert into testtable values (1,'testchar1','testvarchar1','01-JAN-2001','testlong1')</font>&quot;<font color="#6b59ce">)</font><font color="#a52829"><b>!=</b></font>DB_OK<font color="#6b59ce">)</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>...</b></font> process the result set <font color="#a52829"><b>...</b></font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<a name="commit"></a>
<span class="heading2">Commits and Rollbacks</span><br><br>

<p>If you need to execute a commit or rollback, you should use the 
commit() and rollback() functions rather than sending a 
"commit" or "rollback" query.  There are two reasons for this.  First, it's 
much more efficient to call the functions.  Second, if you're writing code that 
can run on transactional or non-transactional databases, some non-transactional
databases will throw errors if they receive a "commit" or "rollback" query, 
but by calling the commit() and rollback() functions you 
instruct the database connection daemon to call the commit and rollback API 
functions for that database rather than issuing them as queries.  If the API's 
have no commit or rollback functions, the calls do nothing and the database 
throws no error.  This is especially important when using SQL Relay with 
ODBC.</p>

<p>You can also turn Autocommit on or off by passing true or false to the
autoCommit() function.</p>

<p>The following command turns Autocommit on.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>autoCommit<font color="#6b59ce">(</font><font color="#ff00ff">true</font><font color="#6b59ce">)</font>;
</pre>
</blockquote>

<p>The following command turns Autocommit off.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>autoCommit<font color="#6b59ce">(</font><font color="#ff00ff">false</font><font color="#6b59ce">)</font>;
</pre>
</blockquote>

<p>When Autocommit is on, the database performs a commit after each successful
DML or DDL query.  When Autocommit is off, the database commits when the client
instructs it to, or (by default) when a client disconnects.  For databases that
don't support Autocommit, autoCommit() has no effect.</p>

<a name="temptables"></a>
<span class="heading2">Temporary Tables</span><br><br>

<p>Some databases support temporary tables.  That is, tables which are
automatically dropped or truncated when an application closes its connection
to the database or when a transaction is committed or rolled back.</p>

<p>For databases which drop or truncate tables when a transaction is committed
or rolled back, temporary tables work naturally.</p>

<p>However, for databases which drop or truncate tables when an application
closes its connection to the database, there is an issue.  Since SQL Relay
maintains persistent database connections, when an application disconnects from
SQL Relay, the connection between SQL Relay and the database remains, so
the database does not know to drop or truncate the table.  To remedy this
situation, SQL Relay parses each query to see if it created a temporary table,
keeps a list of temporary tables and drops (or truncates them) when the
application disconnects from SQL Relay.  Since each database has slightly
different syntax for creating a temporary table, SQL Relay parses each query
according to the rules for that database.</p>

<p>In effect, temporary tables should work when an application connects to
SQL Relay in the same manner that they would work if the application connected
directly to the database.</p>

<a name="errors"></a>
<span class="heading2">Catching Errors</span><br><br>

<p>For most functions, you can find out if an error occurred by calling
DB::isError() on the result.</p>

<p>When running DML or DDL queries, the query() function should return DB_OK.
If it returns anything else, then that also indicates that an error has
occurred.  When running a select, query() returns a result set and DB::isError()
must be used to determine if an error has occurred.</p>

<p>After determining that an error has occurred you can find out why by
calling getMessage().</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">create table testtable (testnumber number, testchar char(40), testvarchar varchar2(40), testdate date, testlong long)</font>&quot;<font color="#6b59ce">)</font><font color="#a52829"><b>!=</b></font>DB_OK<font color="#6b59ce">)</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">insert into testtable values (1,'testchar1','testvarchar1','01-JAN-2001','testlong1')</font>&quot;<font color="#6b59ce">)</font><font color="#a52829"><b>!=</b></font>DB_OK<font color="#6b59ce">)</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>...</b></font> process the result set <font color="#a52829"><b>...</b></font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<a name="bindvars"></a>
<span class="heading2">Bind Variables</span><br><br>

<p>Programs rarely execute fixed queries.  More often than not, some part
of the query is dynamically generated.  The Pear DB API provides means for
using bind variables in those queries.</p>

<p>For a detailed discussion of substitutions and binds, see
<a href="binds.html">this document</a>.

<p>Here is an example where an associative array is used to bind a set of values
to a set of variables.  The values in the array are the values that will be
substituted in place of the bind variables.  The keys in the array may refer to
either the name or (1-based) position of the bind variable.  The query must be
run by calling prepare() and execute().  The array is passed as the second
parameter of the call to execute().</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">insert into testtable values (:var1,:var2,:var3,:var4,:var5)</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">2</font>,
                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testchar2</font>&quot;,
                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testvarchar2</font>&quot;,
                &quot;<font color="#ff00ff">4</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">01-JAN-2002</font>&quot;,
                &quot;<font color="#ff00ff">5</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testlong2</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>When passing a floating point number in as a bind or substitution variable,
you have to supply precision and scale for the number.  See
<a href="precisionscale.html">this page</a> for a discussion of precision and
scale.</p>

<a name="rebinding"></a>
<span class="heading2">Re-Binding and Re-Execution</span><br><br>

<p>Another feature of the prepare/bind/execute paradigm is the ability to 
prepare, bind and execute a query once, then re-bind and re-execute the query 
over and over without re-preparing it.  If your backend database natively 
supports this paradigm, you can reap a substantial performance improvement.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">insert into testtable values (:var1,:var2,:var3,:var4,:var5)</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">2</font>,
                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testchar2</font>&quot;,
                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testvarchar2</font>&quot;,
                &quot;<font color="#ff00ff">4</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">01-JAN-2002</font>&quot;,
                &quot;<font color="#ff00ff">5</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testlong2</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">var1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">3</font>,
                &quot;<font color="#ff00ff">var2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testchar3</font>&quot;,
                &quot;<font color="#ff00ff">var3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testvarchar3</font>&quot;,
                &quot;<font color="#ff00ff">var4</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">01-JAN-2003</font>&quot;,
                &quot;<font color="#ff00ff">var5</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testlong3</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">4</font>,
                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testchar4</font>&quot;,
                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testvarchar4</font>&quot;,
                &quot;<font color="#ff00ff">4</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">01-JAN-2004</font>&quot;,
                &quot;<font color="#ff00ff">5</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testlong4</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">var1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">5</font>,
                &quot;<font color="#ff00ff">var2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testchar5</font>&quot;,
                &quot;<font color="#ff00ff">var3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testvarchar5</font>&quot;,
                &quot;<font color="#ff00ff">var4</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">01-JAN-2005</font>&quot;,
                &quot;<font color="#ff00ff">var5</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">testlong5</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;


<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<a name="fields"></a>
<span class="heading2">Accessing Fields in the Result Set</span><br><br>

<p>The fetchRow() and fetchInto() methods are useful for processing result
sets.  fetchRow() returns a row and fetchInto() populates a pre-allocated row.
Both can be used to return ordered or associative arrays.  The parameter
to determine whether the array is ordered or associative may be passed directly
into the call or (when using fetchRow()) may be set previously using
setFetchMode().</p>

<p>Here is an example using fetchRow() to return an ordered array.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#0000ff"># fetch the next row using ordered mode</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>setFetchMode<font color="#6b59ce">(</font>DB_FETCHMODE_ORDERED<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">4</font><font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#0000ff"># fetch the next row passing ordered mode into fetchRow()</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">(</font>DB_FETCHMODE_ORDERED<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">4</font><font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#0000ff"># fetch row 7 using ordered mode</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">(</font>DB_FETCHMODE_ORDERED,<font color="#ff00ff">7</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">4</font><font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>Here is an example using fetchRow() to return an associative array.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#0000ff"># fetch the next row using ordered mode</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>setFetchMode<font color="#6b59ce">(</font>DB_FETCHMODE_ASSOC<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTLONG</font>'<font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#0000ff"># fetch the next row passing ordered mode into fetchRow()</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">(</font>DB_FETCHMODE_ASSOC<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTLONG</font>'<font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#0000ff"># fetch row 7 using ordered mode</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">(</font>DB_FETCHMODE_ASSOC,<font color="#ff00ff">7</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTLONG</font>'<font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>Here is an example using fetchInto() to populate an ordered array.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#0000ff"># fetch the next row passing ordered mode into fetchInto()</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchInto<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font>,DB_FETCHMODE_ASSOC<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">4</font><font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#0000ff"># fetch row 7 using ordered mode</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchInto<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font>,DB_FETCHMODE_ASSOC,<font color="#ff00ff">7</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">4</font><font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>Here is an example using fetchInto() to populate an associative array.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>query<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#0000ff"># fetch the next row passing ordered mode into fetchInto()</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchInto<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font>,DB_FETCHMODE_ASSOC<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTLONG</font>'<font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#0000ff"># fetch row 7 using ordered mode</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchInto<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font>,DB_FETCHMODE_ASSOC,<font color="#ff00ff">7</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field0</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">field4</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTLONG</font>'<font color="#6b59ce">]</font>;

<font color="#a52829"><b>...</b></font> <font color="#a52829"><b>do</b></font> something with the fields <font color="#a52829"><b>...</b></font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>The Pear DB API also provides convenience functions for fetching an entire
result set, single row, single column or single value from a query, all in one
step.  setFetchMode() applies to some of these functions as well.</p>

<p>getAll() runs a query and fetches the entire result set, all in one step.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#0000ff"># get an ordered array using getAll</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>setFetchMode<font color="#6b59ce">(</font>DB_FETCHMODE_ORDERED<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">rows</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getAll<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">row 0:</font><font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font><font color="#ff00ff">3</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>...</b></font> etc <font color="#a52829"><b>...</b></font>
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">row 1:</font><font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font><font color="#ff00ff">3</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>...</b></font> etc <font color="#a52829"><b>...</b></font>


<font color="#0000ff"># get an associative array using getAll</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>setFetchMode<font color="#6b59ce">(</font>DB_FETCHMODE_ASSOC<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">rows</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getAll<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">row 0:</font><font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>...</b></font> etc <font color="#a52829"><b>...</b></font>
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">row 1:</font><font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>...</b></font> etc <font color="#a52829"><b>...</b></font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>getRow() runs a query and fetches the first row of the result set, all in
one step.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#0000ff"># get an ordered array using getRow</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>setFetchMode<font color="#6b59ce">(</font>DB_FETCHMODE_ORDERED<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getRow<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;


<font color="#0000ff"># get an associative array using getRow</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>setFetchMode<font color="#6b59ce">(</font>DB_FETCHMODE_ASSOC<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getRow<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>getCol() runs a query and fetches one entire column of the result set, all
in one step.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">col</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getCol<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">col</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">col</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">col</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#ff00ff"> </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">col</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>getOne() runs a query and fetches the first column of the first row, all
in one step.</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">field</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getOne<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select count(*) from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#008a8c">echo</font><font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">field</font><font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<a name="cursors"></a>
<span class="heading2">Cursors</span><br><br>

<p>Cursors make it possible to execute queries while processing the result
set of another query.  You can select rows from a table in one query, then 
iterate through its result set, inserting rows into another table, using only 
1 database connection for both operations.</p>

<p>For example:</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">query</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from my_first_table</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>for</b></font> <font color="#6b59ce">(</font><font color="#a52829"><b>var</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">count</font><font color="#a52829"><b>=</b></font><font color="#ff00ff">0</font>; <font color="#a52829"><b>$</b></font><font color="#008a8c">count</font><font color="#a52829"><b>&lt;</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>numRows<font color="#6b59ce">()</font>; <font color="#a52829"><b>$</b></font><font color="#008a8c">count</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">count</font><font color="#a52829"><b>+</b></font><font color="#ff00ff">1</font><font color="#6b59ce">)</font> <font color="#6b59ce">{</font>
        <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()</font>;
        <font color="#a52829"><b>var</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>,
                                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font>,
                                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">row</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">])</font>;
        <font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">insert into my_second_table values (:var1,:var2,:var3)</font>&quot;<font color="#6b59ce">)</font>;
        execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,bindvars<font color="#6b59ce">)</font>;
        <font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<a name="columns"></a>
<span class="heading2">Getting Column Information</span><br><br>

<p>After executing a query, the column count may be retrieved using numCols().
Column data, including column name, type and length are available in the
associative array returned from tableInfo().  tableInfo() takes a parameter
indicating what values to populate the array with.  This parameter may be
empty, DB_TABLEINFO_ORDER, DB_TABLEINFO_ORDERTABLE or both values or'ed
together.</p>

<p>An example calling numCols():</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">query</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">column count: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>numCols<font color="#6b59ce">()</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>An example calling tableInfo():</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">query</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>tableInfo<font color="#6b59ce">()</font>;
<font color="#008a8c">echo</font> &quot;<font color="#ff00ff">column 1:</font><font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">name: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font>'<font color="#ff00ff">name</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">type: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font>'<font color="#ff00ff">type</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">len:  </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">0</font><font color="#6b59ce">][</font>'<font color="#ff00ff">len</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font> &quot;<font color="#ff00ff">column 2:</font><font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">name: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font>'<font color="#ff00ff">name</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">type: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font>'<font color="#ff00ff">type</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">len:  </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">1</font><font color="#6b59ce">][</font>'<font color="#ff00ff">len</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font> &quot;<font color="#ff00ff">column 3:</font><font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">name: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">][</font>'<font color="#ff00ff">name</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">type: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">][</font>'<font color="#ff00ff">type</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">len:  </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">2</font><font color="#6b59ce">][</font>'<font color="#ff00ff">len</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font> &quot;<font color="#ff00ff">column 4:</font><font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">name: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">][</font>'<font color="#ff00ff">name</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">type: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">][</font>'<font color="#ff00ff">type</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">len:  </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">3</font><font color="#6b59ce">][</font>'<font color="#ff00ff">len</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font> &quot;<font color="#ff00ff">column 5:</font><font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">name: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">4</font><font color="#6b59ce">][</font>'<font color="#ff00ff">name</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">type: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">4</font><font color="#6b59ce">][</font>'<font color="#ff00ff">type</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">len:  </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font><font color="#ff00ff">4</font><font color="#6b59ce">][</font>'<font color="#ff00ff">len</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font><font color="#ff00ff">4</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>An example calling tableInfo(DB_TABLEINFO_ORDER):</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">query</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>tableInfo<font color="#6b59ce">(</font>DB_TABLEINFO_ORDER<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">num_fields: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">num_fields</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testnumber is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testchar is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testvarchar is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testdate is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testlong is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTLONG</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;


<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>An example calling tableInfo(DB_TABLEINFO_ORDERTABLE):</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">query</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>tableInfo<font color="#6b59ce">(</font>DB_TABLEINFO_ORDERTABLE<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testnumber is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testchar is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testvarchar is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testdate is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testlong is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTLONG</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<p>An example calling tableInfo(DB_TABLEINFO_ORDER|DB_TABLEINFO_ORDERTABLE):</p>

<blockquote>
<pre>
<font color="#6b59ce">&lt;?php</font>

<font color="#a520f7">require_once</font> '<font color="#ff00ff">DB.php</font>';

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font> <font color="#a52829"><b>=</b></font> DB<font color="#a52829"><b>::</b></font>connect<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">sqlrelay://testuser:testpassword@testhost:9000/testdb</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>if</b></font> <font color="#6b59ce">(</font>DB<font color="#a52829"><b>::</b></font>isError<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#6b59ce">))</font> <font color="#6b59ce">{</font>
        <font color="#008a8c">die</font> <font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>getMessage<font color="#6b59ce">())</font>;
<font color="#6b59ce">}</font>

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">query</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testtable</font>&quot;<font color="#6b59ce">)</font>;

<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">num_fields: </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">num_fields</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testnumber is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testchar is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testvarchar is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testdate is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testlong is field # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">order</font>'<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTLONG</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;

<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testnumber is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTNUMBER</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testchar is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testvarchar is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTVARCHAR</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testdate is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTDATE</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;
<font color="#008a8c">echo</font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">testlong is column # </font>&quot; <font color="#a52829"><b>.</b></font> <font color="#a52829"><b>$</b></font><font color="#008a8c">tableinfo</font><font color="#6b59ce">[</font>'<font color="#ff00ff">ordertable</font>'<font color="#6b59ce">][</font>''<font color="#6b59ce">][</font>'<font color="#ff00ff">TESTLONG</font>'<font color="#6b59ce">]</font> <font color="#a52829"><b>.</b></font> &quot;<font color="#6b59ce">\n</font>&quot;<font color="#6b59ce">)</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>free<font color="#6b59ce">()</font>;

<font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>disconnect<font color="#6b59ce">()</font>;

<font color="#6b59ce">?&gt;</font>
</pre>
</blockquote>

<a name="storedprocedures"></a>
<span class="heading2">Stored Procedures</span><br><br>

<p>Many databases support stored procedures.  Stored procedures are sets of
queries and procedural code that are executed inside of the database itself.
For example, a stored procedure may select rows from one table, iterate through
the result set and, based on the values in each row, insert, update or delete
rows in other tables.  A client program could do this as well, but a stored
procedure is generally more efficient because queries and result sets don't
have to be sent back and forth between the client and database.  Also, stored
procedures are generally stored in the database in a compiled state, while
queries may have to be re-parsed and re-compiled each time they are sent.</p>

<p>While many databases support stored procedures.  The syntax for creating
and executing stored procedures varies greatly between databases.</p>

<p>SQL Relay supports stored procedures for most databases, but there are
some caveats.  Stored procedures are not currently supported when using FreeTDS
against Sybase or Microsoft SQL Server.  Blob/Clob bind variables are only
supported in Oracle 8i or higher.  Sybase stored procedures must use varchar
output parameters.</p>

<p>Stored procedures typically take input paramters from client programs through
input bind variables and return values back to client programs either through
bind variables or result sets.  Stored procedures can be broken down into
several categories, based on the values that they return.  Some stored
procedures don't return any values, some return a single value, some return
multiple values and some return entire result sets.</p>

<span class="heading3">No Values</span><br>

<p>Some stored procedures don't return any values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in2 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in3 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>varchar2</b></font>) <font color="#6b59ce">is</font>
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (in1,in2,in3);
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">begin testproc(:in1,:in2,:in3); end;</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">in1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">in2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">in3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc @in1 int, @in2 <font color="#298a52"><b>float</b></font>, @in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>) <font color="#6b59ce">as</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (@in1,@in2,@in3)
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">exec testproc</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">in1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">in2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">in3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">Firebird</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#298a52"><b>integer</b></font>, in2 <font color="#298a52"><b>float</b></font>, in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (in1,in2,in3);
        suspend;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">exec procedure testproc ?,?,?</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">DB2</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(<font color="#a52829"><b>in</b></font> in1 int, <font color="#a52829"><b>in</b></font> in2 double, <font color="#a52829"><b>in</b></font> in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) language sql
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (in1,in2,in3);
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">call testproc(?,?,?)</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns void <font color="#6b59ce">as</font> '
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> ($<font color="#ff00ff">1</font>,$<font color="#ff00ff">2</font>,$<font color="#ff00ff">3</font>);
        <font color="#6b59ce">return</font>;
<font color="#6b59ce">end</font>;' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select testfunc(:in1,:in2,:in3)</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">in1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">in2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">in3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>))
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>in</b></font> in1 int, <font color="#a52a2a"><b>in</b></font> in2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>in</b></font> in3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>))
<font color="#6a5acd">begin</font>
        <font color="#a52a2a"><b>insert</b></font> <font color="#6a5acd">into</font> mytable <font color="#6a5acd">values</font> (in1,in2,in3);
<font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>MySQL 4.1.2 supports native input binds with its Statement API but the
Statement API cannot be used to call stored procedures yet.  As a result,
SQL Relay must fake input binds when calling MySQL stored procedures, so queries
which call stored procedures must be written with colon-delimited bind
variables rather than MySQL's native ?'s.</p>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">call testproc(:in1,:in2,:in3)</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">in1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">in2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">in3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>




<br><span class="heading3">Single Values</span><br>

<p>Some stored procedures return single values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testproc(in1 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in2 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in3 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>varchar2</b></font>) returns <font color="#298a52"><b>number</b></font> <font color="#6b59ce">is</font>
<font color="#6b59ce">begin</font>
        <font color="#6b59ce">return</font> in1;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select testproc(:in1,:in2,:in3) from dual</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">in1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">in2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">in3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">result</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testproc
</pre>
</blockquote>

<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>In Sybase and Microsoft SQL Server, stored procedures return values
through output parameters rather than as return values of the procedure
itself.  However, the SQL Relay PHP Pear DB driver does not currently support
output parameters.</p>


<span class="heading4">Firebird</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#298a52"><b>integer</b></font>, in2 <font color="#298a52"><b>float</b></font>, in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns (out1 <font color="#298a52"><b>integer</b></font>) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        out1=in1;
        suspend;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testproc(:in1,:in2,:in3)</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">result</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">DB2</span><br>

<p>In DB2, stored procedures return values through output parameters rather
than as return values of the procedure itself.  However, the SQL Relay PHP
Pear DB driver does not currently support output parameters.</p>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>)) returns int <font color="#6b59ce">as</font> '
declare
        in1 int;
        in2 <font color="#298a52"><b>float</b></font>;
        in3 <font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>);
<font color="#6b59ce">begin</font>
        in1:=$<font color="#ff00ff">1</font>;
        <font color="#6b59ce">return</font>;
<font color="#6b59ce">end</font>;
' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testfunc(:in1,:in2,:in3)</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">result</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>))
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>A single value can be returned from a MySQL function.</p>

<p>To create the function, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">function</font> testfunc(<font color="#a52a2a"><b>in</b></font> in1 int, <font color="#a52a2a"><b>in</b></font> in2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>in</b></font> in3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns int <font color="#6a5acd">return</font> in1;
</pre>
</blockquote>

<p>To execute the function from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select testfunc(?,?,?)</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">result</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
</pre>
</blockquote>

<p>To drop the function, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">function</font> testfunc
</pre>
</blockquote>

<p>A single value can be returned in the result set of a MySQL procedure.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc() <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> <font color="#ff00ff">1</font>; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>prepare<font color="#6a5acd">(</font>&quot;<font color="#ff00ff">select testfunc()</font>&quot;<font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>execute<font color="#6a5acd">(</font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">result</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#2e8b57"><b>-&gt;</b></font>fetchRow<font color="#6a5acd">()[</font><font color="#ff00ff">0</font><font color="#6a5acd">]</font>;
</pre>
</blockquote>

<p>To create the procedure, run a query like the following.</p>

<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<p>A single value can be returned using the output variable of a MySQL
procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>out</b></font> out1 int) <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> <font color="#ff00ff">1</font> <font color="#6a5acd">into</font> out1; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>prepare<font color="#6a5acd">(</font>&quot;<font color="#ff00ff">set @out1=0</font>&quot;<font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>prepare<font color="#6a5acd">(</font>&quot;<font color="#ff00ff">call testproc(@out1)</font>&quot;<font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>prepare<font color="#6a5acd">(</font>&quot;<font color="#ff00ff">select @out1</font>&quot;<font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>execute<font color="#6a5acd">(</font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">result</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#2e8b57"><b>-&gt;</b></font>fetchRow<font color="#6a5acd">()[</font><font color="#ff00ff">0</font><font color="#6a5acd">]</font>;
</pre>
</blockquote>

<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>






<br><span class="heading3">Multiple Values</span><br>

<p>Some stored procedures return multiple values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>In Oracle, stored procedures can return values through output parameters or
as return values of the procedure itself.  If a procedure needs to return
multiple values, it can return one of them as the return value of the procedure
itself, but the rest must be returned through output parameters.  However, the
SQL Relay PHP Pear DB driver does not currently support output parameters.</p>

<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>In Sybase and Microsoft SQL Server, stored procedures return values
through output parameters rather than as return values of the procedure
itself.  However, the SQL Relay PHP Pear DB driver does not currently support
output parameters.</p>

<span class="heading4">Firebird</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#298a52"><b>integer</b></font>, in2 <font color="#298a52"><b>float</b></font>, in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns (out1 <font color="#298a52"><b>integer</b></font>, out2 <font color="#298a52"><b>float</b></font>, out3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        out1=in1;
        out2=in2;
        out3=in3;
        suspend;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testfunc(?,?,?)</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">out1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">out2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">out3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">DB2</span><br>

<p>In DB2, stored procedures return values through output parameters rather
than as return values of the procedure itself.  However, the SQL Relay PHP Pear
DB driver does not currently support output parameters.</p>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>)) returns record <font color="#6b59ce">as</font> '
declare
        output record;
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>select</b></font> $<font color="#ff00ff">1</font>,$<font color="#ff00ff">2</font>,$<font color="#ff00ff">3</font> <font color="#6b59ce">into</font> output;
        <font color="#6b59ce">return</font> output;
<font color="#6b59ce">end</font>;
' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testfunc(:in1,:in2,:in3) as (col1 int, col2 float, col3 char(20))</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">in1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">in2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">in3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">out1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">out2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">out3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>))
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>Here's how you can get multiple values from the result
set of a MySQL procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>in</b></font> in1 int, <font color="#a52a2a"><b>in</b></font> in2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>in</b></font> in3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> in1, in2, in3; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>MySQL 4.1.2 supports native input binds with its Statement API but the
Statement API cannot be used to call stored procedures yet.  As a result,
SQL Relay must fake input binds when calling MySQL stored procedures, so queries
which call stored procedures must be written with colon-delimited bind
variables rather than MySQL's native ?'s.</p>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">call testfunc(:in1,:in2,:in3)</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#a52829"><b>=</b></font><font color="#298a52"><b>array</b></font><font color="#6b59ce">(</font>&quot;<font color="#ff00ff">in1</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font>,
                &quot;<font color="#ff00ff">in2</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> <font color="#ff00ff">1</font><font color="#ff00ff">.1</font>,
                &quot;<font color="#ff00ff">in3</font>&quot; <font color="#a52829"><b>=</b></font><font color="#a52829"><b>&gt;</b></font> &quot;<font color="#ff00ff">hello</font>&quot;<font color="#6b59ce">})</font>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font>,<font color="#a52829"><b>$</b></font><font color="#008a8c">bindvars</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">out1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">0</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">out2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">1</font><font color="#6b59ce">]</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">out3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()[</font><font color="#ff00ff">2</font><font color="#6b59ce">]</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<p>Here's how you can get multiple values from the output variables
of a MySQL procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>out</b></font> out1 int, <font color="#a52a2a"><b>out</b></font> out2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>out</b></font> out3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> <font color="#ff00ff">1</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">'hello'</font> <font color="#6a5acd">into</font> out1, out2, out3; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>prepare<font color="#6a5acd">(</font>&quot;<font color="#ff00ff">set @out1=0, @out2=0.0, @out3=''</font>&quot;<font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>execute<font color="#6a5acd">(</font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>prepare<font color="#6a5acd">(</font>&quot;<font color="#ff00ff">call testfunc(@out1,@out2,@out3)</font>&quot;<font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>execute<font color="#6a5acd">(</font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>prepare<font color="#6a5acd">(</font>&quot;<font color="#ff00ff">select @out1,@out2,@out3</font>&quot;<font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>execute<font color="#6a5acd">(</font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">out1</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#2e8b57"><b>-&gt;</b></font>fetchRow<font color="#6a5acd">()[</font><font color="#ff00ff">0</font><font color="#6a5acd">]</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">out2</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#2e8b57"><b>-&gt;</b></font>fetchRow<font color="#6a5acd">()[</font><font color="#ff00ff">1</font><font color="#6a5acd">]</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">out3</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#2e8b57"><b>-&gt;</b></font>fetchRow<font color="#6a5acd">()[</font><font color="#ff00ff">2</font><font color="#6a5acd">]</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>



<br><span class="heading3">Result Sets</span><br>

<p>Some stored procedures return entire result sets.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>Stored procedures in Oracle can return open cursors as return values or
output parameters.  A client-side cursor can be bound to this open cursor and
rows can be fetched from it.  However, the SQL Relay PHP Pear DB driver does not
currently support output parameters.</p>

<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc <font color="#6a5acd">as</font> <font color="#a52a2a"><b>select</b></font> * <font color="#6a5acd">from</font> testtable
</pre>
</blockquote>

<p>To exceute the stored procedure from an SQL Relay program, ue code like the
following.</p>

<blockquote>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">Firebird</span><br>

<p>Stored procedures in Firebird can return a result set if a
select query in the procedure selects values into the output parameters and
then issues a suspend command, however SQL Relay doesn't currently support
stored procedures that return result sets.</p>

<span class="heading4">DB2</span><br>

<p>Stored procedures in DB2 can return a result set if the procedure is declared
to return one, however SQL Relay doesn't currently support stored procedures
that return result sets.</p>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc() returns setof record <font color="#6b59ce">as</font> '
        declare output record;
<font color="#6b59ce">begin</font>
        <font color="#6b59ce">for</font> output <font color="#a52829"><b>in</b></font> <font color="#a52829"><b>select</b></font> * <font color="#6b59ce">from</font> mytable <font color="#6b59ce">loop</font>
                <font color="#6b59ce">return</font> next output;
        <font color="#6b59ce">end</font> <font color="#6b59ce">loop</font>;
        <font color="#6b59ce">return</font>;
<font color="#6b59ce">end</font>;
' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>prepare<font color="#6b59ce">(</font>&quot;<font color="#ff00ff">select * from testfunc() as (col1 int, col2 float, col3 char(20))</font>&quot;<font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">db</font><font color="#298a52"><b>-&gt;</b></font>execute<font color="#6b59ce">(</font><font color="#a52829"><b>$</b></font><font color="#008a8c">sth</font><font color="#6b59ce">)</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">row1</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">row2</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()</font>;
<font color="#a52829"><b>$</b></font><font color="#008a8c">row3</font><font color="#a52829"><b>=</b></font><font color="#a52829"><b>$</b></font><font color="#008a8c">res</font><font color="#298a52"><b>-&gt;</b></font>fetchRow<font color="#6b59ce">()</font>;
<font color="#a52829"><b>...</b></font>
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>The result sets of all select statements called within MySQL stored
procedures (that aren't selected into variables) are returned from the procedure
call.  Though MySQL stored procedures can return multiple result sets,
currently SQL Relay can only fetch the first result set.</p>

<p>To create the stored procedure which returns a result set, run a query like
the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc() <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> * <font color="#6a5acd">from</font> mytable; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>MySQL 4.1.2 supports native input binds with its Statement API but the
Statement API cannot be used to call stored procedures yet.  As a result,
SQL Relay must fake input binds when calling MySQL stored procedures, so queries
which call stored procedures must be written with colon-delimited bind
variables rather than MySQL's native ?'s.</p>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>prepare<font color="#6a5acd">(</font>&quot;<font color="#ff00ff">call testfunc()</font>&quot;<font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">db</font><font color="#2e8b57"><b>-&gt;</b></font>execute<font color="#6a5acd">(</font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">sth</font><font color="#6a5acd">)</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">row1</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#2e8b57"><b>-&gt;</b></font>fetchRow<font color="#6a5acd">()</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">row2</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#2e8b57"><b>-&gt;</b></font>fetchRow<font color="#6a5acd">()</font>;
<font color="#a52a2a"><b>$</b></font><font color="#008b8b">row3</font><font color="#a52a2a"><b>=</b></font><font color="#a52a2a"><b>$</b></font><font color="#008b8b">res</font><font color="#2e8b57"><b>-&gt;</b></font>fetchRow<font color="#6a5acd">()</font>;
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<a name="lastinsertid"></a>
<span class="heading2">Getting the Last Insert ID in SQLite</span><br><br>

<p>The SQLite API provides a function which returns the "last insert rowid";
the rowid of the most recent insert into the database or 0 if no inserts have 
ever been performed during the session.</p>

<p>SQL Relay supports fetching the last insert rowid from an SQLite database
as well.  You just have to run the query:

<blockquote>
<b>select last insert rowid</b>
</blockquote>


</body>
</html>
